import useGetUserInfo from './useGetUserInfo'
import { useEffect } from 'react'
import { useLocation, useNavigate } from 'react-router-dom'
import { isLoginOrRegistry, MANAGE_INDEX_PATHNAME, LOGIN_PATHNAME, isNoNeedLogin } from '../router'
function useNavPage(waitingUserData: boolean) {
  const { username } = useGetUserInfo()
  const { pathname } = useLocation()
  const nav = useNavigate()
  useEffect(() => {
    if (waitingUserData) return
    console.log('hello world', username)
    if (username && username !== '') {
      if (isLoginOrRegistry(pathname)) {
        nav(MANAGE_INDEX_PATHNAME)
      }
    } else {
      if (isNoNeedLogin(pathname)) {
        return
      } else {
        nav(LOGIN_PATHNAME)
      }
    }
  }, [waitingUserData, username, pathname])
}

export default useNavPage
